package net.bwie.realtime.jtp.jdrealtimeservice.controller;

import com.alibaba.fastjson.JSONObject;
import net.bwie.realtime.jtp.jdrealtimeservice.bean.ProvinceTotalPaymentWindowReport;
import net.bwie.realtime.jtp.jdrealtimeservice.bean.TopNitemWindowReport;
import net.bwie.realtime.jtp.jdrealtimeservice.service.ProvinceTotalPaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 简介说明:
 *
 * @author: LiLi
 * @date: 2025/05/28 12:51:59
 * @version: 1.0
 */
@RestController
@RequestMapping("jtp/province")
public class ProvinceTotalController {
    @Autowired
    ProvinceTotalPaymentService provinceTotalPaymentService;

    private static final String DATE_DEFAULT_VALUE = "1970-07-01";
//  1 页面浏览请求拦截方法

    @RequestMapping("Totalpayment")
    public Object selectTotalpayment(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE) String date) {

        List<ProvinceTotalPaymentWindowReport> paymentWindowReports = provinceTotalPaymentService.selectTotalpayment(date);

        if (null == paymentWindowReports || paymentWindowReports.isEmpty()) {
            return "[]";
        }
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("status", 0);
        map.put("msg", "");

        Map<String, Object> data = new LinkedHashMap<>();
        List<Map<String, Object>> mapData = new ArrayList<>();

        // 添加你的数据到 mapData
        for (ProvinceTotalPaymentWindowReport provinceTotalPaymentWindowReport : paymentWindowReports) {
            Map<String, Object> item = new LinkedHashMap<>();
            item.put("name", provinceTotalPaymentWindowReport.getProvince());
            item.put("value", provinceTotalPaymentWindowReport.getTotal_payment());
            mapData.add(item);
        }

        data.put("mapData", mapData);
        map.put("data", data);

        JSONObject jsonObject = new JSONObject(map);


        return jsonObject;

    }

    @RequestMapping("selectTopNitem")
    public String GetPvCount(@RequestParam(value = "date", defaultValue = DATE_DEFAULT_VALUE) String date) {
        // 1. 获取原始数据
        List<TopNitemWindowReport> dataList = provinceTotalPaymentService.selectTopNitem(date);

        // 2. 创建响应对象
        JSONObject response = new JSONObject();
        response.put("status", 0);
        response.put("hitSugarSelf", true);
        response.put("msg", "");

        // 3. 构建列定义
        List<JSONObject> columns = new ArrayList<>();

        JSONObject col1 = new JSONObject();
        col1.put("name", "项目");
        col1.put("id", "item");
        columns.add(col1);

        JSONObject col2 = new JSONObject();
        col2.put("name", "项目大小");
        col2.put("id", "item_size");
        columns.add(col2);

        // 4. 构建轮播数据行
        List<JSONObject> rows = new ArrayList<>();
        int index = 0;

        for (TopNitemWindowReport item : dataList) {
            JSONObject row = new JSONObject();
            row.put("item", item.getItem());
            row.put("item_size", item.getItem_size());

            // 添加轮播控制字段
            row.put("active", index == 0); // 第一项默认激活
            row.put("duration", 3000);    // 3秒轮播间隔
            row.put("index", index++);    // 索引值

            rows.add(row);
        }

        // 5. 组装数据
        JSONObject data = new JSONObject();
        data.put("columns", columns);
        data.put("rows", rows);
        response.put("data", data);

        return response.toJSONString();
    }


}


